package com.glory.hikvisionmobile.adapter;

import android.content.res.Resources;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.base.App;
import com.glory.hikvisionmobile.bean.EqpManger.EqpKeepInputListBean;
import com.glory.hikvisionmobile.bean.EqpManger.EqpKeepSelectorListBean;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EqpKeepInputListAdapter extends BaseQuickAdapter<EqpKeepInputListBean, BaseViewHolder> {
    private List<String> outData = new ArrayList<>();

    public EqpKeepInputListAdapter(@Nullable List<EqpKeepInputListBean> data) {
        super(R.layout.adapter_keep_check_list,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, EqpKeepInputListBean item) {
        helper.setText(R.id.tvItem1,item.getDataName() + "(" + item.getMinValue() + "/" + item.getMaxValue() + ")");
        helper.setText(R.id.tvItem2,item.getDescription());
        helper.setGone(R.id.tvItem3,true);
        helper.setText(R.id.tvItem3,item.getDataType());
        helper.setGone(R.id.etRemarks,true);
        EditText et = helper.getView(R.id.etRemarks);
        if (item.getDataType().equals("NUM")){
            et.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        }
        if (et.getTag() instanceof TextWatcher) {
            et.removeTextChangedListener((TextWatcher) et.getTag());
        }
        et.setText(item.getDataString());
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()>0)
                {
                    try {
                        double value = Double.parseDouble(s.toString());
                        compare(helper.getAdapterPosition(),value,item.getMaxValue(),item.getMinValue(),et);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setDataString(s.toString());
            }
        };
        try {
            double value = Double.parseDouble(item.getDataString());
            compare(helper.getAdapterPosition(),value,item.getMaxValue(),item.getMinValue(),et);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        et.addTextChangedListener(watcher);
        et.setTag(watcher);

    }


    private void compare(int position,double value, String maxValue,String minValue,EditText editText){
        if (!FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
            double maxValues = Double.parseDouble(maxValue);
            double minValues = Double.parseDouble(minValue);
            if(minValues<=value&&value<=maxValues) {
                editText.setTextColor(App.app.getResources().getColor(R.color.black));
                if (outData.contains(String.valueOf(position))){
                    outData.remove(String.valueOf(position));
                }
            }else{
                editText.setTextColor(App.app.getResources().getColor(R.color.red));
                if (!outData.contains(String.valueOf(position))){
                    outData.add(String.valueOf(position));
                }
            }
        }else if (FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
            double minValue1 = Double.parseDouble(minValue);
            if (value >= minValue1){
                editText.setTextColor(App.app.getResources().getColor(R.color.black));
                if (outData.contains(String.valueOf(position))){
                    outData.remove(String.valueOf(position));
                }
            }else{
                editText.setTextColor(App.app.getResources().getColor(R.color.red));
                if (!outData.contains(String.valueOf(position))){
                    outData.add(String.valueOf(position));
                }
            }
        }else if (!FreeApi_StringUtils.isEmpty(maxValue)&&FreeApi_StringUtils.isEmpty(minValue)){
            double maxValue1 = Double.parseDouble(maxValue);
            if (value <= maxValue1){
                editText.setTextColor(App.app.getResources().getColor(R.color.black));
                if (outData.contains(String.valueOf(position))){
                    outData.remove(String.valueOf(position));
                }
            }else{
                editText.setTextColor(App.app.getResources().getColor(R.color.red));
                if (!outData.contains(String.valueOf(position))){
                    outData.add(String.valueOf(position));
                }
            }
        }else {
            editText.setTextColor(App.app.getResources().getColor(R.color.black));
        }
    }

    public List<String> getOutData(){
        return outData;
    }

    public void clearData(){
        outData.clear();
    }

}
